package com.ease.common.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.FileUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ClassUtils;

import com.ease.common.test.TestSupport;

/**
 * 
 * @author nathanleewei
 * 
 */
public abstract class FileUtil extends Detect {

	public static String escapeSafeWindowsFileName(String fileName) {
		if (null != fileName) {
			return fileName.replaceAll("[\\/:*?\"<>|]", "");
		}
		return null;
	}

	public static String readClassPathResourceToString(Class<?> classPackageAsResourcePath, String fileName) {
		return readClassPathResourceToString(ClassUtils.classPackageAsResourcePath(classPackageAsResourcePath), fileName);
	}

	public static String readClassPathResourceToString(String filePath, String fileName) {
		String fullFilePath = filePath + '/' + fileName;
		return readClassPathResourceToString(fullFilePath);
	}

	public static String readFileSystemResourceToString(String fileSystemResourcePath) {
		Resource resource = new FileSystemResource(fileSystemResourcePath);
		return readResourceToString(resource);
	}

	public static String readClassPathResourceToString(String classPathResourcePath) {
		Resource resource = new ClassPathResource(classPathResourcePath);
		return readResourceToString(resource);
	}

	public static String readResourceToString(Resource resource) {
		// ClassPathResource classPathResource = null;
		InputStream inputStream = null;

		try {
			// classPathResource = new ClassPathResource(fullFilePath);
			inputStream = resource.getInputStream();
			// 读取不了文件中的中文内容,所以修改 begin
			// return IOUtils.readStringFromStream(inputStream);

			// TODO org.apache.commons.io.IoUtils.toString(inputStream, "UTF-8")
			return IoUtil.readStringFromStream(inputStream);
			// 读取不了文件中的中文内容,所以修改 end
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != inputStream) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				inputStream = null;
			}
			// if (null != classPathResource) {
			// classPathResource = null;
			// }
		}
		return null;
	}

	public static void write(String folder, String name, String content) {
		BufferedWriter bufferedWriter = null;
		try {
			File filePath = new File(folder);
			if (!filePath.exists()) {
				FileUtils.forceMkdir(filePath);
			}
			File file = new File(folder, name);
			if (null != file) {
				file.createNewFile();
			}
			/** 只读不能修改 */
			file.setWritable(true);
			bufferedWriter = new BufferedWriter(new FileWriter(file));
			bufferedWriter.write(content);
			bufferedWriter.close();
			filePath.exists();

		} catch (Exception e) {
			// System.out.println("path=" + path + ", name=" + name);
			e.printStackTrace();
		} finally {
			try {
				if (null != bufferedWriter) {
					bufferedWriter.close();
					bufferedWriter = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void write(String folder, String name, byte[] bytes) {
		OutputStream outputStream = null;
		try {
			File filePath = new File(folder);
			if (!filePath.exists()) {
				FileUtils.forceMkdir(filePath);
			}
			outputStream = new FileOutputStream(folder + name);
			if (null != outputStream) {
				outputStream.write(bytes);
				outputStream.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != outputStream) {
					outputStream.close();
					outputStream = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		TestSupport.p(FileUtil.escapeSafeWindowsFileName("~!@#$%^&*()_+}{:?><-=[];',./`'"));
	}
}
